<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>xv6 实验1 Unix Utilities | kiloGrand</title><meta name="keywords" content="xv6"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Lab util: Uinx utilities https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.S081&#x2F;2020&#x2F;schedule.html https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.828&#x2F;2020&#x2F;labs&#x2F;util.html  sleep在 UPROGS 项中最后一行添加 $U&#x2F;_sleep\123456789101112131415161718UPROGS&#x3D;\">
<meta property="og:type" content="article">
<meta property="og:title" content="xv6 实验1 Unix Utilities">
<meta property="og:url" content="https://kilogrand.gitee.io/2022/09/01/xv6-lab1/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="Lab util: Uinx utilities https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.S081&#x2F;2020&#x2F;schedule.html https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.828&#x2F;2020&#x2F;labs&#x2F;util.html  sleep在 UPROGS 项中最后一行添加 $U&#x2F;_sleep\123456789101112131415161718UPROGS&#x3D;\">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/coding.jpg">
<meta property="article:published_time" content="2022-09-01T01:41:10.000Z">
<meta property="article:modified_time" content="2022-10-17T06:24:05.938Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="xv6">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/coding.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2022/09/01/xv6-lab1/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'xv6 实验1 Unix Utilities',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-17 14:24:05'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/coding.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">xv6 实验1 Unix Utilities</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-01T01:41:10.000Z" title="发表于 2022-09-01 09:41:10">2022-09-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-10-17T06:24:05.938Z" title="更新于 2022-10-17 14:24:05">2022-10-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>15分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="xv6 实验1 Unix Utilities"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Lab-util-Uinx-utilities"><a href="#Lab-util-Uinx-utilities" class="headerlink" title="Lab util: Uinx utilities"></a>Lab util: Uinx utilities</h1><ul>
<li><a target="_blank" rel="noopener" href="https://pdos.csail.mit.edu/6.S081/2020/schedule.html">https://pdos.csail.mit.edu/6.S081/2020/schedule.html</a></li>
<li><a target="_blank" rel="noopener" href="https://pdos.csail.mit.edu/6.828/2020/labs/util.html">https://pdos.csail.mit.edu/6.828/2020/labs/util.html</a></li>
</ul>
<h2 id="sleep"><a href="#sleep" class="headerlink" title="sleep"></a>sleep</h2><p>在 UPROGS 项中最后一行添加 $U/_sleep\<br><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">UPROGS=\</span><br><span class="line">	$U/_cat\</span><br><span class="line">	$U/_echo\</span><br><span class="line">	$U/_forktest\</span><br><span class="line">	$U/_grep\</span><br><span class="line">	$U/_init\</span><br><span class="line">	$U/_kill\</span><br><span class="line">	$U/_ln\</span><br><span class="line">	$U/_ls\</span><br><span class="line">	$U/_mkdir\</span><br><span class="line">	$U/_rm\</span><br><span class="line">	$U/_sh\</span><br><span class="line">	$U/_stressfs\</span><br><span class="line">	$U/_usertests\</span><br><span class="line">	$U/_grind\</span><br><span class="line">	$U/_wc\</span><br><span class="line">	$U/_zombie\</span><br><span class="line">	$U/_sleep\</span><br></pre></td></tr></table></figure></p>
<p>sleep.c<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/types.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/stat.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;user/user.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(argc == <span class="number">2</span>) &#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;(nothing happens for a little while)\n&quot;</span>);</span><br><span class="line">		sleep(atoi((<span class="type">const</span> <span class="type">char</span>*) argv[<span class="number">1</span>])); <span class="comment">// convert ascii to integer and use system call sleep</span></span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;Usage:sleep number...\n&quot;</span>);  <span class="comment">// put the error masssage to the file descriptor 2</span></span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">1</span>); 	<span class="comment">// terminate the process, </span></span><br><span class="line">				    <span class="comment">// status = 1: indicates failure, and reported to wait() </span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="pingpong"><a href="#pingpong" class="headerlink" title="pingpong"></a>pingpong</h2><h3 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h3><p>通过<code>fork</code>、<code>pipe</code>和<code>getpid</code>系统调用，来实现pingpong</p>
<ul>
<li>fork在父进程中的返回值是pid，在子进程中的返回值是0</li>
<li>pipe初始化的管道，其数组的第一个元素是读端，第二个元素是写端</li>
</ul>
<h3 id="实现"><a href="#实现" class="headerlink" title="实现"></a>实现</h3><p>在 UPROGS 项中最后一行添加 $U/_pingpong\</p>
<p>pingpong.c<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/types.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/stat.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;user/user.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> p1[<span class="number">2</span>]; <span class="comment">// parent -&gt; child</span></span><br><span class="line">	<span class="type">int</span> p2[<span class="number">2</span>]; <span class="comment">// child -&gt; parent</span></span><br><span class="line">	<span class="type">char</span> buffer[] = &#123;<span class="string">&#x27;x&#x27;</span>&#125;;</span><br><span class="line">	<span class="type">int</span> length = <span class="keyword">sizeof</span>(buffer);</span><br><span class="line">	<span class="keyword">if</span>(argc != <span class="number">1</span>)&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;Usage: pingpong\n&quot;</span>);</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	pipe(p1);</span><br><span class="line">	pipe(p2);</span><br><span class="line">	<span class="keyword">if</span>(fork() == <span class="number">0</span>)&#123; <span class="comment">// child</span></span><br><span class="line">		close(p1[<span class="number">1</span>]);</span><br><span class="line">		close(p2[<span class="number">0</span>]);</span><br><span class="line">		<span class="keyword">if</span>(read(p1[<span class="number">0</span>], buffer, length) == <span class="number">1</span>)&#123;  <span class="comment">// child receives the byte</span></span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d: received ping\n&quot;</span>, getpid());</span><br><span class="line">			write(p2[<span class="number">1</span>], buffer, length);     <span class="comment">// child sends the byte to child</span></span><br><span class="line">			close(p2[<span class="number">1</span>]);</span><br><span class="line">			<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">		&#125;</span><br><span class="line">		close(p2[<span class="number">1</span>]);</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">1</span>);			</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;  <span class="comment">// parent</span></span><br><span class="line">		close(p1[<span class="number">0</span>]);</span><br><span class="line">		close(p2[<span class="number">1</span>]);</span><br><span class="line">		write(p1[<span class="number">1</span>], buffer,  length); <span class="comment">// parent sends a byte to the child</span></span><br><span class="line">		close(p1[<span class="number">1</span>]);</span><br><span class="line">		wait((<span class="type">int</span> *)<span class="number">0</span>);</span><br><span class="line">		<span class="keyword">if</span>(read(p2[<span class="number">0</span>], buffer, length) == <span class="number">1</span>)&#123;  <span class="comment">// parent receives the byte</span></span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d: received pong\n&quot;</span>, getpid());</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="primes"><a href="#primes" class="headerlink" title="primes"></a>primes</h2><h3 id="分析-1"><a href="#分析-1" class="headerlink" title="分析"></a>分析</h3><p><img src="/img/xv6-lab1-primes.png" alt="进程链"></p>
<h3 id="实现-1"><a href="#实现-1" class="headerlink" title="实现"></a>实现</h3><p>在 UPROGS 项中最后一行添加 $U/_primes\</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/types.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;user/user.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">close_pipe</span><span class="params">(<span class="type">int</span> *p)</span> &#123;</span><br><span class="line">	close(p[<span class="number">0</span>]);</span><br><span class="line">	close(p[<span class="number">1</span>]);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">primes</span><span class="params">()</span> &#123;</span><br><span class="line">	<span class="type">int</span> n, p, len;</span><br><span class="line">	<span class="type">int</span> fd[<span class="number">2</span>];</span><br><span class="line"></span><br><span class="line">	<span class="comment">// read the frist number from previous prime </span></span><br><span class="line">	<span class="keyword">if</span> ((len = read(<span class="number">0</span>, &amp;n, <span class="keyword">sizeof</span>(<span class="type">int</span>))) &lt;= <span class="number">0</span> || n &lt;= <span class="number">0</span>) &#123;</span><br><span class="line">		close(<span class="number">1</span>);</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">  </span><br><span class="line">	<span class="comment">// print first number to console</span></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;prime %d\n&quot;</span>, n);</span><br><span class="line">  </span><br><span class="line">	pipe(fd);</span><br><span class="line">	<span class="keyword">if</span> (fork() == <span class="number">0</span>) &#123; <span class="comment">// child process</span></span><br><span class="line">		close(<span class="number">0</span>);</span><br><span class="line">		dup(fd[<span class="number">0</span>]);  <span class="comment">// link the std i/o 0 to fd[0] </span></span><br><span class="line">		close_pipe(fd);</span><br><span class="line">		primes();</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;      <span class="comment">// parent process</span></span><br><span class="line">		close(<span class="number">1</span>);</span><br><span class="line">		dup(fd[<span class="number">1</span>]); <span class="comment">// link the std i/o 1 to fd[1]</span></span><br><span class="line">		close_pipe(fd); </span><br><span class="line">		<span class="keyword">while</span> ((len = read(<span class="number">0</span>, &amp;p, <span class="keyword">sizeof</span>(<span class="type">int</span>))) &gt; <span class="number">0</span> &amp;&amp; p &gt; <span class="number">0</span>) &#123;  <span class="comment">// read the number from the prime</span></span><br><span class="line">			<span class="keyword">if</span> (p % n != <span class="number">0</span>) &#123;</span><br><span class="line"> 				write(<span class="number">1</span>, &amp;p, <span class="keyword">sizeof</span>(<span class="type">int</span>));  <span class="comment">// filter the number</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		close(<span class="number">1</span>);</span><br><span class="line">		wait((<span class="type">int</span> *)<span class="number">0</span>);   </span><br><span class="line">	&#125; </span><br><span class="line">	<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">main</span><span class="params">(<span class="type">void</span>)</span> &#123;</span><br><span class="line">	<span class="type">int</span> i;</span><br><span class="line">	<span class="type">int</span> fd[<span class="number">2</span>];</span><br><span class="line">  </span><br><span class="line">	pipe(fd);</span><br><span class="line">	<span class="keyword">if</span> (fork() == <span class="number">0</span>) &#123;  <span class="comment">// child process</span></span><br><span class="line">		close(<span class="number">0</span>);</span><br><span class="line">		dup(fd[<span class="number">0</span>]);  <span class="comment">// link the std i/o 0 to the fd[0]</span></span><br><span class="line">		close_pipe(fd);</span><br><span class="line"> 		primes();</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		close(<span class="number">1</span>);</span><br><span class="line">		dup(fd[<span class="number">1</span>]);  <span class="comment">// link the std i/o 1 to the fd[1]</span></span><br><span class="line">		close_pipe(fd);</span><br><span class="line">		<span class="keyword">for</span> (i = <span class="number">2</span>; i &lt;= <span class="number">35</span>; i++) &#123;</span><br><span class="line">			write(<span class="number">1</span>, &amp;i, <span class="keyword">sizeof</span>(<span class="type">int</span>));</span><br><span class="line">		&#125;</span><br><span class="line">		close(<span class="number">1</span>);</span><br><span class="line">		wait((<span class="type">int</span> *)<span class="number">0</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="find"><a href="#find" class="headerlink" title="find"></a>find</h2><p>在 UPROGS 项中最后一行添加 $U/_find\</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/types.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/stat.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;user/user.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/fs.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span>*</span><br><span class="line"><span class="title function_">fmtname</span><span class="params">(<span class="type">char</span> *path)</span>  <span class="comment">// 将路径格式化为文件名</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">static</span> <span class="type">char</span> buf[DIRSIZ+<span class="number">1</span>];</span><br><span class="line">	<span class="type">char</span> *p;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Find first character after last slash. 从字符串末尾开始遍历，直到遇到&#x27;/&#x27;或者到字符串的头部为止</span></span><br><span class="line">	<span class="keyword">for</span>(p=path+<span class="built_in">strlen</span>(path); p &gt;= path &amp;&amp; *p != <span class="string">&#x27;/&#x27;</span>; p--) ;</span><br><span class="line">	p++;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// Return blank-padded name.</span></span><br><span class="line">	<span class="keyword">if</span>(<span class="built_in">strlen</span>(p) &gt;= DIRSIZ)  <span class="comment">// 字符串太长了</span></span><br><span class="line">		<span class="keyword">return</span> p;</span><br><span class="line">	memmove(buf, p, <span class="built_in">strlen</span>(p)+<span class="number">1</span>);</span><br><span class="line">	<span class="keyword">return</span> buf;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">find</span><span class="params">(<span class="type">char</span> *path, <span class="type">char</span> *file)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> buf[<span class="number">512</span>], *p;</span><br><span class="line">	<span class="type">int</span> fd;</span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">dirent</span> <span class="title">de</span>;</span></span><br><span class="line">	<span class="class"><span class="keyword">struct</span> <span class="title">stat</span> <span class="title">st</span>;</span></span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>((fd = open(path, <span class="number">0</span>)) &lt; <span class="number">0</span>)&#123;  <span class="comment">// 路径不存在</span></span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;find: cannot open %s\n&quot;</span>, path);</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">if</span>(fstat(fd, &amp;st) &lt; <span class="number">0</span>)&#123;   <span class="comment">// 获取不了该路径的状态</span></span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;find: cannot stat %s\n&quot;</span>, path);</span><br><span class="line">		close(fd);</span><br><span class="line">		<span class="keyword">return</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">switch</span>(st.type)&#123;</span><br><span class="line">		<span class="keyword">case</span> T_FILE:  <span class="comment">// 如果这是一个文件</span></span><br><span class="line">			<span class="keyword">if</span>(<span class="built_in">strcmp</span>(fmtname(path), file) == <span class="number">0</span>)&#123;</span><br><span class="line">				<span class="built_in">printf</span>(<span class="string">&quot;%s\n&quot;</span>, path);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">case</span> T_DIR: <span class="comment">// 如果这是一个目录</span></span><br><span class="line">			<span class="keyword">if</span>(<span class="built_in">strlen</span>(path) + <span class="number">1</span> + DIRSIZ + <span class="number">1</span> &gt; <span class="keyword">sizeof</span>(buf))&#123;  <span class="comment">// 路径太长，缓冲区装不下</span></span><br><span class="line">      				<span class="built_in">printf</span>(<span class="string">&quot;find: path too long\n&quot;</span>);</span><br><span class="line">      				<span class="keyword">break</span>;</span><br><span class="line">    			&#125;</span><br><span class="line">    			<span class="built_in">strcpy</span>(buf, path); </span><br><span class="line">			p = buf+<span class="built_in">strlen</span>(buf);</span><br><span class="line">			*p++ = <span class="string">&#x27;/&#x27;</span>;  <span class="comment">// 在路径末尾添加&#x27;/&#x27;</span></span><br><span class="line">			<span class="keyword">while</span>(read(fd, &amp;de, <span class="keyword">sizeof</span>(de)) == <span class="keyword">sizeof</span>(de))&#123;  <span class="comment">// 从该路径读取出文件名或者目录名</span></span><br><span class="line">				<span class="keyword">if</span>(de.inum == <span class="number">0</span> || de.inum == <span class="number">1</span>)</span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				<span class="keyword">if</span> (<span class="built_in">strcmp</span>(de.name, <span class="string">&quot;.&quot;</span>) == <span class="number">0</span> || <span class="built_in">strcmp</span>(de.name, <span class="string">&quot;..&quot;</span>) == <span class="number">0</span>)</span><br><span class="line">					<span class="keyword">continue</span>;</span><br><span class="line">				memmove(p, de.name, DIRSIZ);  <span class="comment">// 在路径后面添加文件名or目录</span></span><br><span class="line">				p[DIRSIZ] = <span class="number">0</span>;      <span class="comment">// the end of string is &#x27;\0&#x27;</span></span><br><span class="line">				find(buf, file);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	close(fd);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">if</span>(argc != <span class="number">3</span>)&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;Usage: find &lt;path&gt; &lt;file&gt;\n&quot;</span>);</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	find(argv[<span class="number">1</span>], argv[<span class="number">2</span>]);</span><br><span class="line">	<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="xargs"><a href="#xargs" class="headerlink" title="xargs"></a>xargs</h2><p>在 UPROGS 项中最后一行添加 $U/_xargs\</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/types.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;user/user.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;kernel/param.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">char</span> *argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> *arguments[MAXARG];</span><br><span class="line">	<span class="type">char</span> buff[<span class="number">255</span>];</span><br><span class="line">	<span class="type">char</span> *p = buff, *q = buff;</span><br><span class="line">	<span class="type">int</span> i, offset;</span><br><span class="line">	<span class="keyword">if</span>(argc &lt;= <span class="number">1</span>)&#123;</span><br><span class="line">		<span class="built_in">fprintf</span>(<span class="number">2</span>, <span class="string">&quot;Usage: xargc &lt;command&gt;...\n&quot;</span>);</span><br><span class="line">		<span class="built_in">exit</span>(<span class="number">1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i = <span class="number">1</span>; i &lt; argc; i++)&#123;  <span class="comment">// get the command which is behind the &quot;xargs&quot;</span></span><br><span class="line">  		arguments[i<span class="number">-1</span>] = argv[i];</span><br><span class="line">	&#125;</span><br><span class="line">	i = i - <span class="number">1</span>;</span><br><span class="line">	offset = i;  <span class="comment">// backup</span></span><br><span class="line">    </span><br><span class="line">	<span class="keyword">while</span>(read(<span class="number">0</span>, p, <span class="number">1</span>))&#123;</span><br><span class="line">		<span class="keyword">if</span>(*p == <span class="string">&#x27;\n&#x27;</span>)&#123;  <span class="comment">// meet the &#x27;\n&#x27; and execute it with arguments</span></span><br><span class="line">    			*p = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">    			arguments[i] = q;</span><br><span class="line">    			<span class="keyword">if</span>(fork() == <span class="number">0</span>)&#123;</span><br><span class="line">    				exec(arguments[<span class="number">0</span>], arguments);</span><br><span class="line">    				<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">    			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    				wait((<span class="type">int</span>*)<span class="number">0</span>);</span><br><span class="line">    				p = buff;</span><br><span class="line">    				q = p;</span><br><span class="line">    				i = offset;</span><br><span class="line">    			&#125;</span><br><span class="line">    		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(*p == <span class="string">&#x27; &#x27;</span> || *p == <span class="string">&#x27;\t&#x27;</span>)&#123;  <span class="comment">// meet the &#x27; &#x27; or &#x27;\t&#x27;, find an argument</span></span><br><span class="line">    			*p = <span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">    			arguments[i] = q;</span><br><span class="line">    			i = i + <span class="number">1</span>;</span><br><span class="line">    			p = p + <span class="number">1</span>;</span><br><span class="line">    			q = p; </span><br><span class="line">    		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">    			p = p + <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="git查看修改记录"><a href="#git查看修改记录" class="headerlink" title="git查看修改记录"></a>git查看修改记录</h2><p>查看文件的变化<br><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">grand@Lubuntu:~/xv6-labs-2020$ git diff origin/util util --stat</span><br><span class="line"> Makefile        |   5 +++++</span><br><span class="line"> gradelib.pyc    | Bin 0 -&gt; 22972 bytes</span><br><span class="line"> user/find.c     |  84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</span><br><span class="line"> user/pingpong.c |  40 ++++++++++++++++++++++++++++++++++++++++</span><br><span class="line"> user/primes.c   |  67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</span><br><span class="line"> user/sleep.c    |  17 +++++++++++++++++</span><br><span class="line"> user/xargs.c    |  47 +++++++++++++++++++++++++++++++++++++++++++++++</span><br><span class="line"> 7 files changed, 260 insertions(+)</span><br></pre></td></tr></table></figure></p>
<hr>
<p>查看具体修改内容<code>git diff origin/util util</code><br><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br></pre></td><td class="code"><pre><span class="line">diff --git a/Makefile b/Makefile</span><br><span class="line">index 0fbaf7c..b2addd4 100644</span><br><span class="line">--- a/Makefile</span><br><span class="line">+++ b/Makefile</span><br><span class="line">@@ -149,6 +149,11 @@ UPROGS=\</span><br><span class="line"> 	$U/_grind\</span><br><span class="line"> 	$U/_wc\</span><br><span class="line"> 	$U/_zombie\</span><br><span class="line">+	$U/_sleep\</span><br><span class="line">+	$U/_pingpong\</span><br><span class="line">+	$U/_find\</span><br><span class="line">+	$U/_xargs\</span><br><span class="line">+	$U/_primes\</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> ifeq ($(LAB),syscall)</span><br><span class="line">diff --git a/gradelib.pyc b/gradelib.pyc</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..634a0f3</span><br><span class="line">Binary files /dev/null and b/gradelib.pyc differ</span><br><span class="line">diff --git a/user/find.c b/user/find.c</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..8070f86</span><br><span class="line">--- /dev/null</span><br><span class="line">+++ b/user/find.c</span><br><span class="line">@@ -0,0 +1,84 @@</span><br><span class="line">+#include &quot;kernel/types.h&quot;</span><br><span class="line">+#include &quot;kernel/stat.h&quot;</span><br><span class="line">+#include &quot;user/user.h&quot;</span><br><span class="line">+#include &quot;kernel/fs.h&quot;</span><br><span class="line">+</span><br><span class="line">+char*</span><br><span class="line">+fmtname(char *path)  // 将路径格式化为文件名</span><br><span class="line">+&#123;</span><br><span class="line">+  static char buf[DIRSIZ+1];</span><br><span class="line">+  char *p;</span><br><span class="line">+</span><br><span class="line">+  // Find first character after last slash. 从字符串末尾开始遍历，直到遇到&#x27;/&#x27;或者到字符串的头部为止</span><br><span class="line">+  for(p=path+strlen(path); p &gt;= path &amp;&amp; *p != &#x27;/&#x27;; p--) ;</span><br><span class="line">+  p++;</span><br><span class="line">+</span><br><span class="line">+  // Return blank-padded name.</span><br><span class="line">+  if(strlen(p) &gt;= DIRSIZ)  // 字符串太长了</span><br><span class="line">+    return p;</span><br><span class="line">+  memmove(buf, p, strlen(p)+1);</span><br><span class="line">+  return buf;</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+void</span><br><span class="line">+find(char *path, char *file)</span><br><span class="line">+&#123;</span><br><span class="line">+  char buf[512], *p;</span><br><span class="line">+  int fd;</span><br><span class="line">+  struct dirent de;</span><br><span class="line">+  struct stat st;</span><br><span class="line">+</span><br><span class="line">+  if((fd = open(path, 0)) &lt; 0)&#123;  // 路径不存在</span><br><span class="line">+    fprintf(2, &quot;find: cannot open %s\n&quot;, path);</span><br><span class="line">+    return;</span><br><span class="line">+  &#125;</span><br><span class="line">+</span><br><span class="line">+  if(fstat(fd, &amp;st) &lt; 0)&#123;   // 获取不了该路径的状态</span><br><span class="line">+    fprintf(2, &quot;find: cannot stat %s\n&quot;, path);</span><br><span class="line">+    close(fd);</span><br><span class="line">+    return;</span><br><span class="line">+  &#125;</span><br><span class="line">+</span><br><span class="line">+  switch(st.type)&#123;</span><br><span class="line">+  case T_FILE:  // 如果这是一个文件</span><br><span class="line">+    	if(strcmp(fmtname(path), file) == 0)&#123;</span><br><span class="line">+			printf(&quot;%s\n&quot;, path);</span><br><span class="line">+	&#125;</span><br><span class="line">+    break;</span><br><span class="line">+</span><br><span class="line">+  case T_DIR: // 如果这是一个目录</span><br><span class="line">+    if(strlen(path) + 1 + DIRSIZ + 1 &gt; sizeof buf)&#123;  // 路径太长，缓冲区装不下</span><br><span class="line">+      printf(&quot;find: path too long\n&quot;);</span><br><span class="line">+      break;</span><br><span class="line">+    &#125;</span><br><span class="line">+    strcpy(buf, path); </span><br><span class="line">+    p = buf+strlen(buf);</span><br><span class="line">+    *p++ = &#x27;/&#x27;;  // 在路径末尾添加&#x27;/&#x27;</span><br><span class="line">+    while(read(fd, &amp;de, sizeof(de)) == sizeof(de))&#123;  // 从该路径读取出文件名或者目录名</span><br><span class="line">+      if(de.inum == 0 || de.inum == 1)</span><br><span class="line">+        continue;</span><br><span class="line">+      if (strcmp(de.name, &quot;.&quot;) == 0 || strcmp(de.name, &quot;..&quot;) == 0)</span><br><span class="line">+        continue;</span><br><span class="line">+      memmove(p, de.name, DIRSIZ);  // 在路径后面添加文件名or目录</span><br><span class="line">+      p[DIRSIZ] = 0;      // the end of string is &#x27;\0&#x27;</span><br><span class="line">+      find(buf, file);</span><br><span class="line">+    &#125;</span><br><span class="line">+    break;</span><br><span class="line">+</span><br><span class="line">+  default:</span><br><span class="line">+    break;</span><br><span class="line">+  &#125;</span><br><span class="line">+  close(fd);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+</span><br><span class="line">+int</span><br><span class="line">+main(int argc, char *argv[])</span><br><span class="line">+&#123;</span><br><span class="line">+	if(argc != 3)&#123;</span><br><span class="line">+		fprintf(2, &quot;Usage: find &lt;path&gt; &lt;file&gt;\n&quot;);</span><br><span class="line">+		exit(1);</span><br><span class="line">+	&#125;</span><br><span class="line">+	find(argv[1], argv[2]);</span><br><span class="line">+	exit(0);</span><br><span class="line">+&#125;</span><br><span class="line">diff --git a/user/pingpong.c b/user/pingpong.c</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..caecc4b</span><br><span class="line">--- /dev/null</span><br><span class="line">+++ b/user/pingpong.c</span><br><span class="line">@@ -0,0 +1,40 @@</span><br><span class="line">+#include &quot;kernel/types.h&quot;</span><br><span class="line">+#include &quot;kernel/stat.h&quot;</span><br><span class="line">+#include &quot;user/user.h&quot;</span><br><span class="line">+</span><br><span class="line">+int</span><br><span class="line">+main(int argc, char *argv[])</span><br><span class="line">+&#123;</span><br><span class="line">+	int p1[2]; // parent -&gt; child</span><br><span class="line">+	int p2[2]; // child -&gt; parent</span><br><span class="line">+    char buffer[] = &#123;&#x27;x&#x27;&#125;;</span><br><span class="line">+    int length = sizeof(buffer);</span><br><span class="line">+	if(argc != 1)&#123;</span><br><span class="line">+		fprintf(2, &quot;Usage: pingpong\n&quot;);</span><br><span class="line">+		exit(1);</span><br><span class="line">+	&#125;</span><br><span class="line">+	pipe(p1);</span><br><span class="line">+	pipe(p2);</span><br><span class="line">+	if(fork() == 0)&#123; // child</span><br><span class="line">+		close(p1[1]);</span><br><span class="line">+		close(p2[0]);</span><br><span class="line">+		if(read(p1[0], buffer, length) == 1)&#123;  // child receives the byte</span><br><span class="line">+			printf(&quot;%d: received ping\n&quot;, getpid());</span><br><span class="line">+			write(p2[1], buffer, length);     // child sends the byte to child</span><br><span class="line">+			close(p2[1]);</span><br><span class="line">+			exit(0);</span><br><span class="line">+		&#125;</span><br><span class="line">+		close(p2[1]);</span><br><span class="line">+		exit(1);			</span><br><span class="line">+	&#125;else&#123;  // parent</span><br><span class="line">+		close(p1[0]);</span><br><span class="line">+		close(p2[1]);</span><br><span class="line">+		write(p1[1], buffer,  length); // parent sends a byte to the child</span><br><span class="line">+		close(p1[1]);</span><br><span class="line">+		wait((int *)0);</span><br><span class="line">+		if(read(p2[0], buffer, length) == 1)&#123;  // parent receives the byte</span><br><span class="line">+			printf(&quot;%d: received pong\n&quot;, getpid());</span><br><span class="line">+		&#125;</span><br><span class="line">+		exit(0);</span><br><span class="line">+	&#125;</span><br><span class="line">+&#125;</span><br><span class="line">diff --git a/user/primes.c b/user/primes.c</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..6f9180e</span><br><span class="line">--- /dev/null</span><br><span class="line">+++ b/user/primes.c</span><br><span class="line">@@ -0,0 +1,67 @@</span><br><span class="line">+#include &quot;kernel/types.h&quot;</span><br><span class="line">+#include &quot;user/user.h&quot;</span><br><span class="line">+</span><br><span class="line">+void</span><br><span class="line">+close_pipe(int *p) &#123;</span><br><span class="line">+  close(p[0]);</span><br><span class="line">+  close(p[1]);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+void</span><br><span class="line">+primes() &#123;</span><br><span class="line">+  int n, p, len;</span><br><span class="line">+  int fd[2];</span><br><span class="line">+</span><br><span class="line">+  // read the frist number from previous prime </span><br><span class="line">+  if ((len = read(0, &amp;n, sizeof(int))) &lt;= 0 || n &lt;= 0) &#123;</span><br><span class="line">+    close(1);</span><br><span class="line">+    exit(0);</span><br><span class="line">+  &#125;</span><br><span class="line">+  </span><br><span class="line">+  // print first number to console</span><br><span class="line">+  printf(&quot;prime %d\n&quot;, n);</span><br><span class="line">+  </span><br><span class="line">+  pipe(fd);</span><br><span class="line">+  if (fork() == 0) &#123; // child process</span><br><span class="line">+    close(0);</span><br><span class="line">+    dup(fd[0]);  // link the std i/o 0 to fd[0] </span><br><span class="line">+    close_pipe(fd);</span><br><span class="line">+    primes();</span><br><span class="line">+  &#125; else &#123;      // parent process</span><br><span class="line">+    close(1);</span><br><span class="line">+    dup(fd[1]); // link the std i/o 1 to fd[1]</span><br><span class="line">+    close_pipe(fd); </span><br><span class="line">+    while ((len = read(0, &amp;p, sizeof(int))) &gt; 0 &amp;&amp; p &gt; 0) &#123;  // read the number from the prime</span><br><span class="line">+      if (p % n != 0) &#123;</span><br><span class="line">+        write(1, &amp;p, sizeof(int));  // filter the number</span><br><span class="line">+      &#125;</span><br><span class="line">+    &#125;</span><br><span class="line">+    close(1);</span><br><span class="line">+    wait((int *)0);   </span><br><span class="line">+  &#125; </span><br><span class="line">+  exit(0);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+int</span><br><span class="line">+main(void) &#123;</span><br><span class="line">+  int i;</span><br><span class="line">+  int fd[2];</span><br><span class="line">+  </span><br><span class="line">+  pipe(fd);</span><br><span class="line">+  if (fork() == 0) &#123;  // child process</span><br><span class="line">+    close(0);</span><br><span class="line">+    dup(fd[0]);  // link the std i/o 0 to the fd[0]</span><br><span class="line">+    close_pipe(fd);</span><br><span class="line">+    primes();</span><br><span class="line">+  &#125; else &#123;</span><br><span class="line">+    close(1);</span><br><span class="line">+    dup(fd[1]);  // link the std i/o 1 to the fd[1]</span><br><span class="line">+    close_pipe(fd);</span><br><span class="line">+    for (i = 2; i &lt;= 35; i++) &#123;</span><br><span class="line">+      write(1, &amp;i, sizeof(int));</span><br><span class="line">+    &#125;</span><br><span class="line">+    close(1);</span><br><span class="line">+    wait((int *)0);</span><br><span class="line">+  &#125;</span><br><span class="line">+  exit(0);</span><br><span class="line">+&#125;</span><br><span class="line">diff --git a/user/sleep.c b/user/sleep.c</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..1c4d51d</span><br><span class="line">--- /dev/null</span><br><span class="line">+++ b/user/sleep.c</span><br><span class="line">@@ -0,0 +1,17 @@</span><br><span class="line">+#include &quot;kernel/types.h&quot;</span><br><span class="line">+#include &quot;kernel/stat.h&quot;</span><br><span class="line">+#include &quot;user/user.h&quot;</span><br><span class="line">+</span><br><span class="line">+int</span><br><span class="line">+main(int argc, char *argv[])</span><br><span class="line">+&#123;</span><br><span class="line">+	if(argc == 2)&#123;</span><br><span class="line">+		sleep(atoi((const char*) argv[1])); // convert ascii to integer and use system call sleep</span><br><span class="line">+		exit(0);</span><br><span class="line">+	&#125;</span><br><span class="line">+	else&#123;</span><br><span class="line">+		fprintf(2, &quot;Usage:sleep number...\n&quot;);  // put the error masssage to the file descriptor 2</span><br><span class="line">+		exit(1); 	// terminate the process, </span><br><span class="line">+				//status = 1: indicates failure, and reported to wait() </span><br><span class="line">+	&#125;</span><br><span class="line">+&#125;</span><br><span class="line">diff --git a/user/xargs.c b/user/xargs.c</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..1423af7</span><br><span class="line">--- /dev/null</span><br><span class="line">+++ b/user/xargs.c</span><br><span class="line">@@ -0,0 +1,47 @@</span><br><span class="line">+#include &quot;kernel/types.h&quot;</span><br><span class="line">+#include &quot;user/user.h&quot;</span><br><span class="line">+#include &quot;kernel/param.h&quot;</span><br><span class="line">+</span><br><span class="line">+int</span><br><span class="line">+main(int argc, char *argv[])</span><br><span class="line">+&#123;</span><br><span class="line">+	char *arguments[MAXARG];</span><br><span class="line">+	char buff[255];</span><br><span class="line">+	char *p = buff, *q = buff;</span><br><span class="line">+	int i, offset;</span><br><span class="line">+	if(argc &lt;= 1)&#123;</span><br><span class="line">+		fprintf(2, &quot;Usage: xargc &lt;command&gt;...\n&quot;);</span><br><span class="line">+		exit(1);</span><br><span class="line">+	&#125;</span><br><span class="line">+	</span><br><span class="line">+	for(i = 1; i &lt; argc; i++)&#123;  // get the command which is behind the &quot;xargs&quot;</span><br><span class="line">+        arguments[i-1] = argv[i];</span><br><span class="line">+    &#125;</span><br><span class="line">+    i = i - 1;</span><br><span class="line">+    offset = i;  // backup</span><br><span class="line">+    </span><br><span class="line">+    while(read(0, p, 1))&#123;</span><br><span class="line">+    	if(*p == &#x27;\n&#x27;)&#123;  // meet the &#x27;\n&#x27; and execute it with arguments</span><br><span class="line">+    		*p = &#x27;\0&#x27;;</span><br><span class="line">+    		arguments[i] = q;</span><br><span class="line">+    		if(fork() == 0)&#123;</span><br><span class="line">+    			exec(arguments[0], arguments);</span><br><span class="line">+    			exit(0);</span><br><span class="line">+    		&#125;else&#123;</span><br><span class="line">+    			wait((int*)0);</span><br><span class="line">+    			p = buff;</span><br><span class="line">+    			q = p;</span><br><span class="line">+    			i = offset;</span><br><span class="line">+    		&#125;</span><br><span class="line">+    	&#125;else if(*p == &#x27; &#x27; || *p == &#x27;\t&#x27;)&#123;  // meet the &#x27; &#x27; or &#x27;\t&#x27;, find an argument</span><br><span class="line">+    		*p = &#x27;\0&#x27;;</span><br><span class="line">+    		arguments[i] = q;</span><br><span class="line">+    		i = i + 1;</span><br><span class="line">+    		p = p + 1;</span><br><span class="line">+    		q = p; </span><br><span class="line">+    	&#125;else&#123;</span><br><span class="line">+    		p = p + 1;</span><br><span class="line">+    	&#125;</span><br><span class="line">+    &#125;</span><br><span class="line">+	exit(0);</span><br><span class="line">+&#125;</span><br></pre></td></tr></table></figure></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2022/09/01/xv6-lab1/">https://kilogrand.gitee.io/2022/09/01/xv6-lab1/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/xv6/">xv6</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/08/31/xv6-debug-userprogram/"><img class="prev-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">xv6调试用户程序</div></div></a></div><div class="next-post pull-right"><a href="/2022/09/02/xv6-lab2/"><img class="next-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">xv6 实验2 system calls</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/08/31/xv6-Chapter-0/" title="xv6 riscv book 中文版声明"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 riscv book 中文版声明</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-1/" title="xv6 第一章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第一章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-2/" title="xv6 第二章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第二章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-3/" title="xv6 第三章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第三章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-4/" title="xv6 第四章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第四章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-5/" title="xv6 第五章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第五章</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Lab-util-Uinx-utilities"><span class="toc-number">1.</span> <span class="toc-text">Lab util: Uinx utilities</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#sleep"><span class="toc-number">1.1.</span> <span class="toc-text">sleep</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pingpong"><span class="toc-number">1.2.</span> <span class="toc-text">pingpong</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%9E%90"><span class="toc-number">1.2.1.</span> <span class="toc-text">分析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.2.2.</span> <span class="toc-text">实现</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#primes"><span class="toc-number">1.3.</span> <span class="toc-text">primes</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%9E%90-1"><span class="toc-number">1.3.1.</span> <span class="toc-text">分析</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0-1"><span class="toc-number">1.3.2.</span> <span class="toc-text">实现</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#find"><span class="toc-number">1.4.</span> <span class="toc-text">find</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#xargs"><span class="toc-number">1.5.</span> <span class="toc-text">xargs</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#git%E6%9F%A5%E7%9C%8B%E4%BF%AE%E6%94%B9%E8%AE%B0%E5%BD%95"><span class="toc-number">1.6.</span> <span class="toc-text">git查看修改记录</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>